﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace YJY_DMZS.BasisFile
{
    public partial class FrmDepartmentType_Editor : XtraForm
    {
        public FrmDepartmentType_Editor()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 主键值，非正零为新增
        /// </summary>
        public int ID;
        /// <summary>
        /// 父类型节点
        /// </summary>
        public int ParentID;
        /// <summary>
        /// 标题栏文字格式
        /// </summary>
        private string captionFormat;

        private void FrmDepartment_Editor_Load(object sender, EventArgs e)
        {
            captionFormat = Text;
        }

        private void FrmDepartment_Editor_Shown(object sender, EventArgs e)
        {
            if (ID <= 0)
            {
                NewRecord();
            }
            else
            {
                LoadData(ID);
            }
        }
        /// <summary>
        /// 加载数据
        /// </summary>
        /// <param name="ID"></param>
        private void LoadData(int ID)
        {
            Text = string.Format(captionFormat, "维护");
            var bll = new DMZS.BLL.DepartmentType();
            var dt = bll.GetRecordByID(ID);
            if (dt != null)
            {
                ParentID = Convert.ToInt32(dt.Rows[0]["ParentID"]);
                txtParentNo.Text = dt.Rows[0]["ParentNo"].ToString();
                if (ParentID == 0)
                {
                    txtParentName.Text = "(顶级)";
                }
                else
                {
                    txtParentName.Text = dt.Rows[0]["ParentName"].ToString();
                }
                txtNo.Text = dt.Rows[0]["No"].ToString();
                txtName.Text = dt.Rows[0]["Name"].ToString();
                txtNo.Properties.ReadOnly = true;
                txtName.Focus();
            }
            else
            {
                XtraMessageBox.Show("该条数据已被删除，请刷新数据。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                Close();
            }
        }
        /// <summary>
        /// 新记录
        /// </summary>
        private void NewRecord()
        {
            Text = string.Format(captionFormat, "新增");
            ID = 0;
            txtNo.Properties.ReadOnly = false;

            if (ParentID != 0)
            {
                var bll = new DMZS.BLL.DepartmentType();
                var model = bll.GetModel(ParentID);

                if (model != null)
                {
                    txtParentNo.Text = model.No;
                    txtParentName.Text = model.Name;
                    txtNo.Text = string.Empty;
                    txtName.Text = string.Empty;

                    txtNo.Focus();
                }
                else
                {
                    XtraMessageBox.Show("父类型节点被删除，请刷新数据。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                    Close();
                }
            }
            else
            {
                txtParentNo.Text = string.Empty;
                txtParentName.Text = "(顶级)";
                txtNo.Text = string.Empty;
                txtName.Text = string.Empty;

                txtNo.Focus();
            }
        }
        private void btnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (txtNo.Text.Trim().Length == 0)
            {
                XtraMessageBox.Show("请输入类型编号。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                txtNo.Focus();
                return;
            }
            else
            {
                if (txtName.Text.Trim().Length == 0)
                {
                    XtraMessageBox.Show("请输入类型编号。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                    txtName.Focus();
                    return;
                }
            }
            if (ID <= 0)
            {
                SaveNew();
            }
            else
            {
                SaveModify();
            }
        }
        /// <summary>
        /// 保存修改
        /// </summary>
        private void SaveModify()
        {
            var bll = new DMZS.BLL.DepartmentType();
            var model = new DMZS.Model.DepartmentType();
            model.ID = ID;
            model.No = txtNo.Text.Trim();
            model.Name = txtName.Text.Trim();

            if (bll.Update(model))
            {
                XtraMessageBox.Show("保存成功。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
            }
            else
            {
                XtraMessageBox.Show("保存失败。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
            }
            LoadData(ID);
        }
        /// <summary>
        /// 保存新增
        /// </summary>
        private void SaveNew()
        {
            var bll = new DMZS.BLL.DepartmentType();
            var model = new DMZS.Model.DepartmentType();
            model.No = txtNo.Text.Trim();
            model.Name = txtName.Text.Trim();
            var resutl = bll.AddExpand(model, ParentID);
            if (resutl > 0)
            {
                XtraMessageBox.Show("保存成功。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                ID = resutl;
                LoadData(ID);
            }
            else
            {
                if (resutl == -1001)
                {
                    XtraMessageBox.Show("该类型编号重复。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                    txtNo.Focus();
                    txtNo.SelectAll();
                }
                else
                {
                    XtraMessageBox.Show("保存失败，建议刷新数据。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                }
            }
        }
        private void btnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var bll = new DMZS.BLL.DepartmentType();

            var childrenCount = bll.GetChildrenCount(ID);
            if (childrenCount < 0)
            {
                XtraMessageBox.Show("检索子级数据出错，建议刷新数据。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                return;
            }

            var text = string.Empty;
            if (childrenCount != 0)
            {
                text = string.Format("确认删除,其下有{0}子项？\r注意：将同时删除子项！", childrenCount);
            }
            else
            {
                text = "确认删除？";
            }
            if (DialogResult.No == XtraMessageBox.Show(text, Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1))
            {
                return;
            }
            if (bll.DeleteExpand(ID))
            {
                XtraMessageBox.Show("删除成功。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                NewRecord();
            }
            else
            {
                XtraMessageBox.Show("删除失败，建议刷新数据。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
            }
        }
        private void btnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (ID <= 0)
            {
                return;
            }
            LoadData(ID);
        }
    }
}
